package mianshi;

import java.util.List;

/**
 * Created by JiaJia on 2019/8/20 0020.
 */
public class ProducerConsumer2 {

    static class Consumer implements Runnable{
        private List<Integer> queue;
        public Consumer(List<Integer> queue){
            this.queue=queue;
        }

        @Override
        public void run() {
           try {

               while (true) {
                   synchronized (queue) {
                       while (queue.isEmpty()) {
                           System.out.println("queue is empty!");
                           queue.wait();
                       }
                       int i = queue.remove(0);
                       queue.notifyAll();
                       System.out.println(Thread.currentThread().getName() + "消费了" + i + "还剩" + queue.size());
                       Thread.sleep(100);
                   }
               }
           }catch (Exception e){
               e.printStackTrace();
           }

        }
    }


}